﻿using System;
using System.Web;
using Microsoft.SharePoint.Administration;

namespace Repo.Helpers {
    
    public static class LoggingHelper {

        private const string DefaultCategory = "SPS ";

        private static bool IsVerbose(HttpRequest request)
        {
            var isVerbose = false;
            var isDebug = false;
            if (request["verbose"] != null)
            {
                bool.TryParse(request["verbose"], out isVerbose);
            }
            else if (request.UrlReferrer != null && request.UrlReferrer.Query.ToLower().Contains("verbose=true"))
            {
                isVerbose = true;
            }

            if (request["debug"] != null)
            {
                bool.TryParse(request["debug"], out isDebug);
            }
            else if (request.UrlReferrer != null && request.UrlReferrer.Query.ToLower().Contains("debug=true"))
            {
                isDebug = true;
            }
            return isVerbose || isDebug;
        }

        public static void DebugLog(string message)
        {
            DebugLog(null, message);
        }

        public static void DebugLog(HttpContext context, string message)
        {
            if (IsVerbose(context == null ? ContextHelper.Request : context.Request))
            {
                VerboseLog(message, string.Empty);
            }
        }

        public static void VerboseLog(string message) {
            VerboseLog(message, string.Empty);
        }

        public static void VerboseLog(string message, string category) {
            SPDiagnosticsService.Local.WriteTrace(0,
                                                  new SPDiagnosticsCategory(
                                                      DefaultCategory + category,
                                                      TraceSeverity.Verbose,
                                                      EventSeverity.Information),
                                                  TraceSeverity.Verbose,
                                                  message,
                                                  Environment.StackTrace);
        }

        public static void ErrorLog(Exception exception) {
            ErrorLog(exception, string.Empty);
        }

        public static void ErrorLog(Exception exception, string category) {
            SPDiagnosticsService.Local.WriteTrace(0,
                                                  new SPDiagnosticsCategory(
                                                      DefaultCategory + category,
                                                      TraceSeverity.High,
                                                      EventSeverity.Error),
                                                  TraceSeverity.Unexpected,
                                                  exception.Message,
                                                  exception.StackTrace);
        }
    }

}
